<?php
	include("../lib/DataProvider.php");
	if(isset($_POST['btnThem']))
	{
		$us=$_POST['txtTenDangNhap'];
		$pw=$_POST['txtMatKhau'];
		$email=$_POST['txtEmail'];
		
		//tạo mã người dùng
		$loai="S";
		date_default_timezone_set('Asia/Saigon');		
	 	$now = getdate();
		$head=  $loai.($now["year"] % 100).date("m",$now["mon"]);
			//tạo số thứ tự
		$soluong=DataProvider::ExecuteQuery("select * from nguoidung where MAND like '".$head."'");
		$stt=mysql_num_rows($soluong)+1;
		$stt=sprintf("%04s", $stt);
		$flag=true;
		$MaND=$head.$stt;
		while($flag==true)
		{
			$ds=DataProvider::ExecuteQuery("select * from nguoidung where MAND = '".$MaND."'");
			if(mysql_num_rows($ds)>0)
			{
				$stt=$stt+1;
				$stt=sprintf("%04s", $stt);
				$MaND=$head.$stt;
				$flag=true;
			}else $flag=false;
		}
		$sql1="insert into nguoidung(MAND,TENDANGNHAP,MATKHAU,MALND,EMAIL,KICHHOAT,BIXOA) values('".$MaND."','".$us."','".$pw."','".$loai."','".$email."',1,0)";
		$sql2="insert into hoso(MAHS) values('".$MaND."')";
		$sql3="insert into diachi(MADIACHI) values('".$MaND."')";
		$result1=DataProvider::ExecuteQuery($sql1);
		$result2=DataProvider::ExecuteQuery($sql2);
		$result3=DataProvider::ExecuteQuery($sql3);
		echo '<div class="center"><b>Thêm Seller thành công!</b></div>';
		?>
			<head>
			<meta http-equiv="refresh" content="2; url=?act=2" />
			</head>
		<?php
		return;
	}
	if(isset($_POST['btnCapNhat']))
	{
		$MaND=$_POST['txtMaND'];
		$us=$_POST['txtTenDangNhap'];
		$pw=$_POST['txtMatKhau'];
		$MaLND=$_POST['txtMaLND'];
		$email=$_POST['txtEmail'];
		$kichhoat=0;
		if(isset($_POST['cbKichHoat']))
			$kichhoat=1;
		$bixoa=0;
		if(isset($_POST['cbBiXoa']))
			$bixoa=1;
		$sql="update nguoidung set TENDANGNHAP='".$us."',MATKHAU='".$pw."',MALND='".$MaLND."',EMAIL='".$email."',KICHHOAT=".$kichhoat.",BIXOA=".$bixoa." where MAND='".$MaND."'";
		DataProvider::ExecuteQuery($sql);
		echo '<div class="center"><b>Cập nhật thành công!</b></div>';
		?>
			<head>
			<meta http-equiv="refresh" content="2; url=?act=2" />
			</head>
		<?php
		return;
	}?>
	<script type="text/javascript">
		location="?act=1";
	</script>
	<?php
?>